package com.shidaimanong.demo.encypt;

import java.security.MessageDigest;

/**
 * 学编程，关注：十代码农
 * 抖音、快手、B站、西瓜视频、今日头条
 */
public class ShaDemo {

    public static void main(String[] args) throws Exception {
        String msg = "123456";

        //SHA-1加密
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
        sha1.update(msg.getBytes());
        byte[] sha1Bin = sha1.digest();
        printBytes(sha1Bin);

        //SHA-256加密
        MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
        sha256.update(msg.getBytes());
        byte[] sha256Bin = sha256.digest();
        printBytes(sha256Bin);

    }

    /**
     * 十六进制打印字节数组
     */
    public static void printBytes(byte[] b) {
        for (int i = 0; i < b.length; i++) {
            System.out.printf("%02X", b[i]);
        }
        System.out.println();
    }
}